<template lang="html">
  <div id="charts">
    <div id="stc" :style="{width:'100%',height:'100%'}"></div>
  </div>
</template>

<script>
import co from 'co'
import echarts from 'echarts'
import {HttpResetClass } from 'vue-client'

let getData = function * (self) {
  // let s1 = 't.'
  // let s2 = 'u.'
  // self.replacestr(self.condition,s1)
  // self.replacestr(self.con,s2)
  let load = new HttpResetClass()

  load.load('POST','rs/sql/SellTypechart',
  {data: {condition: self.condition, item: self.item}},
  {resolveMsg: null, rejectMsg: null})
  .then((res) => {
    let arrparams = []
    let gasdata = []
    let pricedata = []
    for (var i = 0; i < res.data.length; i++) {
        arrparams[i] = res.data[i].name
        gasdata[i] = res.data[i].gas_value
        pricedata[i] = res.data[i].price_value
    }
    self.set(arrparams,gasdata,pricedata)
  })
}
export default {
  props: ['condition', 'tablename', 'sum_state'],
 data () {
   return {
     xc: '',
     con: '',
     item: 't.f_payment'
   }
 },
 methods: {
   set (arrparams,gasdata,pricedata) {
     this.xc = echarts.init(document.getElementById('stc'))
     this.xc.setOption({
       title : {
        //  text: '金额气量散列图'
        text: '' // 由于显示遮盖，将标题去掉
       },
       tooltip : {
         trigger: 'axis'
       },
       legend: {
         data: ['气量','金额']
       },
       toolbox: {
         show : true,
         feature : {
           dataView : {show: true, readOnly: false},
           magicType : {show: true, type: ['line', 'bar']},
           restore : {show: true},
           saveAsImage : {show: true}
         }
       },
       calculable : true,
       xAxis : [
         {
           type : 'category',
           data : arrparams
         }
      ],
      yAxis : [
        {
          type : 'value'
        }
      ],
      series : [
        {
          name:'气量',
          type:'bar',
          data:gasdata,
          markPoint : {
            data : [
              {type : 'max', name: '最大值'},
              {type : 'min', name: '最小值'}
            ]
          },
          markLine : {
            data : [
              {type : 'average', name: '平均值'}
            ]
          }
        },
        {
          name:'金额',
          type:'bar',
          data:pricedata,
          markPoint : {
            data : [
              {type : 'max', name: '最大值'},
              {type : 'min', name: '最小值'}
            ]
          },
          markLine : {
          data : [
              {type : 'average', name : '平均值'}
            ]
          }
        }
      ]
     })
   },
   searchdata () {
     let getGen =getData(this)
     co(getGen)
   },
   replacestr (str,rstr) {
     if (str.indexOf(rstr) >= 0) {
       this.replacestr(str.replace(rstr, ''), rstr)
     } else {
       this.con = str
     }
   }
 },
 watch: {
   'condition' (val) {
      this.searchdata()
   },
   'tablename' (val) {
     this.searchdata()
   },
   'sum_state': {
     deep: true,
     handler (val, oldval) {
       if (val[0] === '付款方式') {
         this.item = 't.f_payment'
       } else {
         this.item = 't.f_gasproperties'
       }
       this.searchdata()
     }
   }
 },
 ready () {
   this.con = this.condition
   this.searchdata()
 }

}
</script>
<style lang="css">
#charts div{
  height: auto;
}
</style>
